<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    /*
        1.例子
     */

    let p = new Promise(((resolve, reject) => {
        console.log(1)
        resolve()
    }))
    p.then(() => console.log(2))
    p.then(() => console.log(3))
    p.then(() => console.log(4))
    console.log(5)
    // 上面会打印15234

    /*
        2.异步任务串行化
     */
    let p1 = new Promise(((resolve, reject) => {
        console.log('11')
        setTimeout(resolve, 1000)
    }))
    p1.then(() => new Promise(((resolve, reject) => {
        console.log('12')
        setTimeout(resolve, 1000)
    })))
        .then(() => new Promise(((resolve, reject) => {
            console.log('13')
            setTimeout(resolve, 1000)
        })))
        .then(() => new Promise(((resolve, reject) => {
            console.log('14')
            setTimeout(resolve, 1000)
        })))

    /*
        3. 提取代码到工厂函数
     */
    function delayedResolve(str) {
        return new Promise((resolve, reject) => {
            console.log(str)
            setTimeout(resolve, 1000)
        })
    }

    delayedResolve(101)
        .then(_ => delayedResolve(102))
        .then(_ => delayedResolve(103))
        .then(_ => delayedResolve(104))


</script>
</body>
</html>